#!/bin/sh

# If you want to have core files generated (for debugging purposes):
#ulimit -c unlimited

findMissingSO () {

  LDCONFIG="$1"
  SO_LINKEDLIBNAME="$2"
  SO_LIBLOOKUP1="$3"
  SO_LIBLOOKUP2="$4"

  hasSO=`$LDCONFIG -p | grep -m 1 "$SO_LIBLOOKUP1" | cut "-d>" -f2 | cut "-d " -f2`
  if [ -n "$hasSO" ]; then
    echo 'default library ['"$SO_LINKEDLIBNAME"'] was found in ['"$hasSO"']' >&2
  else
    echo 'default library ['"$SO_LINKEDLIBNAME"'] is missing, attempting to find and link to a newer version if available...' >&2
    hasSO=`$LDCONFIG -p | grep -m 1 "$SO_LIBLOOKUP2" | cut "-d>" -f2 | cut "-d " -f2`
    if [ -n "$hasSO" ]; then
      echo 'new library link ['"$hasSO"'] pointed to from ['"$SO_LINKEDLIBNAME"']' >&2
      ln -f -s $hasSO $SO_LINKEDLIBNAME
    fi
  fi

  return 0
}

# Library directory
LIBDIR='lib'

# As a first step to determine the absolute path of the directory the game 
# resides in, dereference symlinks to this script and convert relative to 
# absolute paths along the way.
MYLOCATION="`readlink -f $0`"

# The game directory is the directory the script resides in
GAMEDIR="`dirname $MYLOCATION`"

# Change to the game dir, and go!
cd $GAMEDIR
echo 'gamedir ['"$GAMEDIR"']' >&2

# export game library directory
test -n "${LIBDIR}" && export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GAMEDIR}/${LIBDIR}"

# ensure ldconfig is found (was an issue on OpenSuSE 11.2)
echo 'Looking for LDCONFIG ['"$LDCONFIG"']...' >&2

if [ "$LDCONFIG"'_' = '_' ]; then    # 'LDCONFIG' environment variable, if set, overrides the following detection 
  echo 'LDCONFIG environment variable is not set...' >&2
  if [ `which ldconfig 2>/dev/null` ]; then 
    LDCONFIG=`which ldconfig 2>/dev/null`
    echo 'Found LDCONFIG using which ['"$LDCONFIG"']...' >&2
  else
    if [ -x /sbin/ldconfig ]; then
      LDCONFIG=/sbin/ldconfig
      echo 'Found LDCONFIG in /sbin ['"$LDCONFIG"']...' >&2
    # optionally add more custom locations using 'elseif' here
    else
      echo 'ERROR: Could not find the "ldconfig" command.' >&2
      echo 'Please re-run using: "LDCONFIG=/path/to/ldconfig '"$0" >&2
      exit 1
    fi
  fi
else
  echo 'Found LDCONFIG environment variable ['"$LDCONFIG"']...' >&2
fi


# now deal with the openal library [libopenal.so.0]
OS_TYPE=`uname -m`

# OpenSuSE (11.3) and Fedora needs its own openAL
#if [ -f /etc/SuSE-release -a -f lib/libopenal* ]; then
if [ -f /etc/SuSE-release ] || [ -f /etc/fedora-release ] ; then
  
  if [ -f /etc/SuSE-release ] ; then
    echo 'Detected Open Suse...' >&2
  fi

  if [ -f /etc/fedora-release ] ; then
    echo 'Detected Fedora...' >&2
  fi

  #rm -f lib/libopenal*
  # libcurl*, libgnu*, libicu*, liblua*, libxerces*
  #mv `ls -1 | grep -v '\.abc$' | grep -v '\.xyz$' | xargs`

  if [ ! -d lib_bkp ]; then
	  mkdir lib_bkp
	  mv lib/* lib_bkp/
	  cp lib_bkp/libcurl* lib/
	  cp lib_bkp/libgnu* lib/
	  cp lib_bkp/libicu* lib/
	  cp lib_bkp/liblua* lib/
	  cp lib_bkp/libxerces* lib/
  fi
fi

OPENAL_LINKEDLIBNAME='libopenal.so.0'
OPENAL_LIBLOOKUP1=${OPENAL_LINKEDLIBNAME}
OPENAL_LIBLOOKUP2='libopenal.so.'

DIRECTFB_LINKEDLIBNAME='libdirectfb-1.0.so.0'
DIRECTFB_LIBLOOKUP1=${DIRECTFB_LINKEDLIBNAME}
DIRECTFB_LIBLOOKUP2='libdirectfb-'

FUSION_LINKEDLIBNAME='libfusion-1.0.so.0'
FUSION_LIBLOOKUP1=${DIRECTFB_LINKEDLIBNAME}
FUSION_LIBLOOKUP2='libfusion-'

DIRECT_LINKEDLIBNAME='libdirect-1.0.so.0'
DIRECT_LIBLOOKUP1=${DIRECT_LINKEDLIBNAME}
DIRECT_LIBLOOKUP2='libdirect-'

JPG_LINKEDLIBNAME='libjpeg.so.62'
JPG_LIBLOOKUP1=${JPG_LINKEDLIBNAME}
JPG_LIBLOOKUP2='libjpeg.so'

PNG_LINKEDLIBNAME='libpng15.so.15'
PNG_LIBLOOKUP1=${PNG_LINKEDLIBNAME}
PNG_LIBLOOKUP2='libpng'

CURL_LINKEDLIBNAME='libcurl.so.4'
CURL_LIBLOOKUP1=${CURL_LINKEDLIBNAME}
CURL_LIBLOOKUP2='libcurl.so.'

XERCES_LINKEDLIBNAME='libxerces-c-3.1.so'
XERCES_LIBLOOKUP1=${XERCES_LINKEDLIBNAME}
XERCES_LIBLOOKUP2='libxerces-c'

ICUDATA_LINKEDLIBNAME='libicudata.so.38'
ICUDATA_LIBLOOKUP1=${ICUDATA_LINKEDLIBNAME}
ICUDATA_LIBLOOKUP2='libicudata.so.'

ICUUC_LINKEDLIBNAME='libicuuc.so.38'
ICUUC_LIBLOOKUP1=${ICUUC_LINKEDLIBNAME}
ICUUC_LIBLOOKUP2='libicuuc.so.'

GLEW_LINKEDLIBNAME='libGLEW.so.1.5'
GLEW_LIBLOOKUP1=${GLEW_LINKEDLIBNAME}
GLEW_LIBLOOKUP2='libGLEW.so.'
GLEW_LIBLOOKUP3='libglew.so.'

OGG_LINKEDLIBNAME='libogg.so.0'
OGG_LIBLOOKUP1=${OGG_LINKEDLIBNAME}
OGG_LIBLOOKUP2='libogg.so.'

VORBIS_LINKEDLIBNAME='libvorbis.so.0'
VORBIS_LIBLOOKUP1=${VORBIS_LINKEDLIBNAME}
VORBIS_LIBLOOKUP2='libvorbis.so.'

VORBISFILE_LINKEDLIBNAME='libvorbisfile.so.3'
VORBISFILE_LIBLOOKUP1=${VORBISFILE_LINKEDLIBNAME}
VORBISFILE_LIBLOOKUP2='libvorbisfile.so.'

FONTCONFIG_LINKEDLIBNAME='libfontconfig.so.1'
FONTCONFIG_LIBLOOKUP1=${FONTCONFIG_LINKEDLIBNAME}
FONTCONFIG_LIBLOOKUP2='libfontconfig.so.'

FREETYPE_LINKEDLIBNAME='libfreetype.so.6'
FREETYPE_LIBLOOKUP1=${FREETYPE_LINKEDLIBNAME}
FREETYPE_LIBLOOKUP2='libfreetype.so.'

GNUTLS_LINKEDLIBNAME='libgnutls.so.26'
GNUTLS_LIBLOOKUP1=${GNUTLS_LINKEDLIBNAME}
GNUTLS_LIBLOOKUP2='libgnutls.so.'

LDAP_LINKEDLIBNAME='libldap_r-2.4.so.2'
LDAP_LIBLOOKUP1=${LDAP_LINKEDLIBNAME}
LDAP_LIBLOOKUP2='libldap_r-'

RESOLV_LINKEDLIBNAME='libresolv.so.2'
RESOLV_LIBLOOKUP1=${RESOLV_LINKEDLIBNAME}
RESOLV_LIBLOOKUP2='libresolv.so.'

LUA_LINKEDLIBNAME='liblua5.1.so.0'
LUA_LIBLOOKUP1=${LUA_LINKEDLIBNAME}
LUA_LIBLOOKUP2='liblua.so.'

FTGL_LINKEDLIBNAME='libftgl.so.2'
FTGL_LIBLOOKUP1=${FTGL_LINKEDLIBNAME}
FTGL_LIBLOOKUP2='libftgl.so.'

IRC_LINKEDLIBNAME='libircclient.so.1'
IRC_LIBLOOKUP1=${IRC_LINKEDLIBNAME}
IRC_LIBLOOKUP2='libircclient.so.'

DL_LINKEDLIBNAME='libdl.so.2'
DL_LIBLOOKUP1=${DL_LINKEDLIBNAME}
DL_LIBLOOKUP2='libdl.so.'

VLC_LINKEDLIBNAME='libvlc.so.5'
VLC_LIBLOOKUP1=${VLC_LINKEDLIBNAME}
VLC_LIBLOOKUP2='libvlc.so.'

VLCCORE_LINKEDLIBNAME='libvlccore.so.5'
VLCCORE_LIBLOOKUP1=${VLCCORE_LINKEDLIBNAME}
VLCCORE_LIBLOOKUP2='libvlccore.so.'

GCRYPT_LINKEDLIBNAME='libgcrypt.so.11'
GCRYPT_LIBLOOKUP1=${GCRYPT_LINKEDLIBNAME}
GCRYPT_LIBLOOKUP2='libgcrypt.so.'

TASN_LINKEDLIBNAME='libtasn1.so.3'
TASN_LIBLOOKUP1=${TASN_LINKEDLIBNAME}
TASN_LIBLOOKUP2='libtasn1.so.'

RTMP_LINKEDLIBNAME='librtmp.so.0'
RTMP_LIBLOOKUP1=${RTMP_LINKEDLIBNAME}
RTMP_LIBLOOKUP2='librtmp.so.'


if [ "$OS_TYPE"'_' = 'x86_64_' ]; then
  DIRECTFB_LINKEDLIBNAME='libdirectfb-1.2.so.0'
  DIRECTFB_LIBLOOKUP1=${DIRECTFB_LINKEDLIBNAME}
  DIRECTFB_LIBLOOKUP2='libdirectfb-'

  FUSION_LINKEDLIBNAME='libfusion-1.2.so.0'
  FUSION_LIBLOOKUP1=${DIRECTFB_LINKEDLIBNAME}
  FUSION_LIBLOOKUP2='libfusion-'

  DIRECT_LINKEDLIBNAME='libdirect-1.2.so.0'
  DIRECT_LIBLOOKUP1=${DIRECT_LINKEDLIBNAME}
  DIRECT_LIBLOOKUP2='libdirect-'

  OPENAL_LINKEDLIBNAME='libopenal.so.1'
  OPENAL_LIBLOOKUP1=${OPENAL_LINKEDLIBNAME}
  OPENAL_LIBLOOKUP2='libopenal.so.'

  ICUDATA_LINKEDLIBNAME='libicudata.so.40'
  ICUDATA_LIBLOOKUP1=${ICUDATA_LINKEDLIBNAME}
  ICUDATA_LIBLOOKUP2='libicudata.so.'

  ICUUC_LINKEDLIBNAME='libicuuc.so.40'
  ICUUC_LIBLOOKUP1=${ICUUC_LINKEDLIBNAME}
  ICUUC_LIBLOOKUP2='libicuuc.so.'

  GNUTLS_LINKEDLIBNAME='libgnutls.so.26'
  GNUTLS_LIBLOOKUP1=${GNUTLS_LINKEDLIBNAME}
  GNUTLS_LIBLOOKUP2='libgnutls.so.'

GCRYPT_LINKEDLIBNAME='libgcrypt.so.11'
GCRYPT_LIBLOOKUP1=${GCRYPT_LINKEDLIBNAME}
GCRYPT_LIBLOOKUP2='libgcrypt.so.'

fi


#if [ "$OS_TYPE"'_' = 'x86_64_' ]; then
#  OPENAL_LIBLOOKUP1="${OPENAL_LIBLOOKUP1}"' (libc6)'
#  OPENAL_LIBLOOKUP2="${OPENAL_LIBLOOKUP2}"' (libc6)'
#
#  DIRECTFB_LIBLOOKUP1="${DIRECTFB_LIBLOOKUP1}"' (libc6)'
#  DIRECTFB_LIBLOOKUP2="${DIRECTFB_LIBLOOKUP2}"' (libc6)'
#
#  FUSION_LIBLOOKUP1="${FUSION_LIBLOOKUP1}"' (libc6)'
#  FUSION_LIBLOOKUP2="${FUSION_LIBLOOKUP2}"' (libc6)'
#
#  DIRECT_LIBLOOKUP1="${DIRECT_LIBLOOKUP1}"' (libc6)'
#  DIRECT_LIBLOOKUP2="${DIRECT_LIBLOOKUP2}"' (libc6)'
#fi

findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${OPENAL_LINKEDLIBNAME}" "$OPENAL_LIBLOOKUP1" "$OPENAL_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${DIRECTFB_LINKEDLIBNAME}" "$DIRECTFB_LIBLOOKUP1" "$DIRECTFB_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${FUSION_LINKEDLIBNAME}" "$FUSION_LIBLOOKUP1" "$FUSION_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${DIRECT_LINKEDLIBNAME}" "$DIRECT_LIBLOOKUP1" "$DIRECT_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${JPG_LINKEDLIBNAME}" "$JPG_LIBLOOKUP1" "$JPG_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${PNG_LINKEDLIBNAME}" "$PNG_LIBLOOKUP1" "$PNG_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${CURL_LINKEDLIBNAME}" "$CURL_LIBLOOKUP1" "$CURL_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${XERCES_LINKEDLIBNAME}" "$XERCES_LIBLOOKUP1" "$XERCES_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${ICUDATA_LINKEDLIBNAME}" "$ICUDATA_LIBLOOKUP1" "$ICUDATA_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${ICUUC_LINKEDLIBNAME}" "$ICUUC_LIBLOOKUP1" "$ICUUC_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${GLEW_LINKEDLIBNAME}" "$GLEW_LIBLOOKUP1" "$GLEW_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${GLEW_LINKEDLIBNAME}" "$GLEW_LIBLOOKUP1" "$GLEW_LIBLOOKUP3"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${OGG_LINKEDLIBNAME}" "$OGG_LIBLOOKUP1" "$OGG_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${VORBIS_LINKEDLIBNAME}" "$VORBIS_LIBLOOKUP1" "$VORBIS_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${VORBISFILE_LINKEDLIBNAME}" "$VORBISFILE_LIBLOOKUP1" "$VORBISFILE_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${FONTCONFIG_LINKEDLIBNAME}" "$FONTCONFIG_LIBLOOKUP1" "$FONTCONFIG_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${FREETYPE_LINKEDLIBNAME}" "$FREETYPE_LIBLOOKUP1" "$FREETYPE_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${GNUTLS_LINKEDLIBNAME}" "$GNUTLS_LIBLOOKUP1" "$GNUTLS_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${LDAP_LINKEDLIBNAME}" "$LDAP_LIBLOOKUP1" "$LDAP_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${RESOLV_LINKEDLIBNAME}" "$RESOLV_LIBLOOKUP1" "$RESOLV_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${LUA_LINKEDLIBNAME}" "$LUA_LIBLOOKUP1" "$LUA_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${FTGL_LINKEDLIBNAME}" "$FTGL_LIBLOOKUP1" "$FTGL_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${IRC_LINKEDLIBNAME}" "$IRC_LIBLOOKUP1" "$IRC_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${DL_LINKEDLIBNAME}" "$DL_LIBLOOKUP1" "$DL_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${VLCCORE_LINKEDLIBNAME}" "$VLCCORE_LIBLOOKUP1" "$VLCCORE_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${VLC_LINKEDLIBNAME}" "$VLC_LIBLOOKUP1" "$VLC_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${GCRYPT_LINKEDLIBNAME}" "$GCRYPT_LIBLOOKUP1" "$GCRYPT_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${TASN_LINKEDLIBNAME}" "$TASN_LIBLOOKUP1" "$TASN_LIBLOOKUP2"
findMissingSO "$LDCONFIG" "$GAMEDIR/$LIBDIR/${RTMP_LINKEDLIBNAME}" "$RTMP_LIBLOOKUP1" "$RTMP_LIBLOOKUP2"

./megaglest $@

# Other ways to start MegaGlest (for testing purposes)
#
# Write verbose output (stout+stderr) to a gzip compressed file (only)
#./megaglest $@ --verbose 2>&1 | gzip > ~/.megaglest/verbose.log.gz
#
# Create a new date/time indexed log file for every MG run and keep it;
# write output (stout+stderr) both to this file and to the screen
#./megaglest $@ 2>&1 | tee -a /tmp/`date +"%F_%k-%M-%S"`.glest.log 2>&1

exit $?
